<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" :tree-data="treeData">
            <el-input v-model="userInfo.departmentName" class="inputW" readonly @focus="departmentNameFocus" />
            <div class="tree-box">
              <!-- 可以使用readonly -->
              <el-tree v-show="showTree" :data="treeData" default-expand-all :props="{ label: 'name' }" @node-click="treeClick" />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <upload-img ref="uploadImg" :staff-photo="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getUserPhotoAPI, updateEmployeesAPI, getDepartmentsListAPI } from '@/api'
import EmployeeEnum from '@/api/constant'
import { transTree } from '@/utils'
export default {
  name: 'UserInfo',
  data() {
    return {
      userInfo: {}, // 个人信息对象
      EmployeeEnum,
      showTree: false,
      treeData: [] // 部门树形控件
    }
  },
  created() {
    this.getUserInfoFn()
    // 获取部门列表数据
    this.getDepartmentsListFn()
  },
  methods: {
    // 请求部门列表
    async getDepartmentsListFn() {
      const res = await getDepartmentsListAPI()
      this.treeData = transTree(res.data.depts, '')
    },
    async getUserInfoFn() {
      const res = await getUserPhotoAPI(this.$route.query.id)
      this.userInfo = res.data
      this.$refs.uploadImg.imageUrl = res.data.staffPhoto
      //   this.userInfo.formOfEmployment = parseInt(this.$route.query.form) 数据反映不到试图上
      // $set()Vue内部提供一个专门更新数组、对象某个值的（并且额外添加数据劫持）
      // 参数1：数组/对象 目标
      // 参数2：下标/属性名
      // 参数3： 值
      this.$set(this.userInfo, 'formOfEmployment', parseInt(this.$route.query.form))
    },
    // 保存更新按钮->点击事件
    async saveUser() {
      this.userInfo.staffPhoto = this.$refs.uploadImg.imageUrl
      const res = await updateEmployeesAPI(this.userInfo)
      this.$message.success(res.message)
      this.$router.back()
    },
    // 部门输入框聚焦事件
    departmentNameFocus() {
      this.showTree = true
    },
    // 树形控件出现
    treeClick(data) { // 点击那一行的数据对象
      if (data && data.children) {
        return // 你点击在了折叠这一行上
      }
      this.userInfo.departmentName = data.name
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
